using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.Designer.BUSView
{
	public class BUSVariablesSymbol : BUSDocumentSymbol
	{
		#region Initialization
		public BUSVariablesSymbol()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			this.MinimumSize = new Size(16, 16);
			this.Size = new Size(50, 16);
			this.TopLevel = true;

			this.Text = "Process Variables";

			this.Border = BorderType.None;

			this.BackColor = Color.FromArgb(128, Color.White);
			this.ForeColor = Color.Black;

			this.NameTextBox.Remove();
			//this.NameTextBox.BackColor = Color.Transparent;

			this.ContextMenuStrip.Items.Clear();

			this.EndUpdate();
		}
		#endregion

		#region Public Members

		#region Methods
		public override void Paint(System.Windows.Forms.PaintEventArgs pe)
		{
			int y = 0;
			Rectangle r = this.GetPaintBounds();
			Point pImg;
			Rectangle rTxt;

			rTxt = new Rectangle(r.X, r.Y, r.Width, cItemHeight);
			using ( StringFormat sf = new StringFormat(BUSDocumentSymbol.TextFormat) ) {
				sf.Alignment = StringAlignment.Center;
				pe.Graphics.DrawString(this.Text, this.Font,
									   Brushes.DarkBlue,
									   rTxt, sf);
			}
			pe.Graphics.DrawLine(Pens.DarkBlue, r.X, r.Y + cItemHeight - 2, r.X + r.Width, r.Y + cItemHeight - 2);
			y += cItemHeight;
			foreach ( BusinessObject bo in this.NamedBusinessObjects.BusinessObjects ) {
				bool selected = ( bo == this.SelectedBusinessObject );

				pImg = this.IsPublishedDocument ?
						new Point(r.X, r.Y + y) :
						new Point(r.X + this.Width - BUSDocumentSymbol.DocumentImage.Width, r.Y + y);

				rTxt = this.IsPublishedDocument ?
						new Rectangle(r.X + this.TextStartPosition, r.Y + y,
									  r.Width - this.TextStartPosition, cItemHeight) :
						new Rectangle(r.X, r.Y + y,
									  r.Width - this.TextStartPosition, cItemHeight);
				if ( selected ) {
					pe.Graphics.FillRectangle(SystemBrushes.Highlight,
						new Rectangle(r.X, r.Y + ( this.SelectedIndex + 1 ) * cItemHeight, r.Width, cItemHeight));
				}
				pe.Graphics.DrawImageUnscaled(BUSDocumentSymbol.DocumentImage, pImg);
				pe.Graphics.DrawString(bo.Name, this.Font,
									   ( selected ? SystemBrushes.HighlightText : this.ForeBrush ),
									   rTxt, BUSDocumentSymbol.TextFormat);

				y += cItemHeight;
			}
		}
		#endregion

		#endregion

		#region Protected Members

		#region Methods
		// override
		protected override int MeasureHeight()
		{
			return cItemHeight * ( this.NamedBusinessObjects.Count + 1 );
		}
		protected override int GetSelectedIndex(System.Windows.Forms.MouseEventArgs e)
		{
			int index = ( e.Y - this.Top ) / cItemHeight;
			if ( index > 0 ) {
				index--;
			} else {
				index = -1;
			}
			return index;
		}
		protected override void OnDesignerViewChanged(EventArgs e)
		{
			base.OnDesignerViewChanged(e);
			this.UpdateSymbol();
		}
		#endregion

		#endregion
	}
}
